/*eslint-disable */
import storageApi from '../../core/storage/storage';

@Inject('$log', '$rootScope', '$scope', '$state', 'iterationTableService')
class IterationDisabledTableController {
  constructor() {
    this.recordId = $state.params.recordId;
    this.type = $state.params.type;

    this.$scope.$on('$viewContentLoaded', () => {
      this.initPreview();
    });
    // dismiss ueditor instantance and dom structure as if leave this page
    this.$scope.$on('$destroy', () => {
      const ue = this.ue_instance;
      ue && ue.destroy && ue.destroy();

      $('#edui_fixedlayer').remove();
      $('#global-zeroclipboard-html-bridge').remove();
    });
  }

  initPreview() {
    this.iterationTableService
      .queryDisabled({ id: this.recordId })
      .then((res) => {
        this.previewInfo = res.data;
        this.getPreviewUE(this.previewInfo);
      });
  }

  getPreviewUE(previewInfo) {
    const ue = this.ue_instance = UE.getEditor('container', {
      toolbars: [
        ['print', 'fullscreen']
      ],
      autoHeightEnabled: true,
      autoFloatEnabled: true,
    });
    UE.ajax.request( '/sgms/api/v1/safetyTable/record/findOne?id=' + this.recordId, {

      //请求方法。可选值： 'GET', 'POST'，默认值是'POST'
      method: 'GET',

      //超时时间。 默认为5000， 单位是ms
      timeout: 10000,

      //是否是异步请求。 true为异步请求， false为同步请求
      async: true,

      //请求携带的数据。如果请求为GET请求， data会经过stringify后附加到请求url之后。
      data: {
        name: 'ueditor'
      },

      //请求成功后的回调， 该回调接受当前的XMLHttpRequest对象作为参数。
      onsuccess: function ( xhr ) {
        ue.ready(function () {
          ue.setContent(previewInfo.content);
        });
      },

      //请求失败或者超时后的回调。
      onerror: function ( xhr ) {
        alert( 'Ajax请求失败' );
      }

    });
  }
}

export default IterationDisabledTableController;
